import {
  queryUser, addUser, updateUser, resetPwd, disableUser, removeUsers,
  queryRole, queryUserRole, setUserRole, addRole, updateRole, queryRight,
  queryRoleRight, setRoleRight
} from '@/services/permission';

export default {
  namespace: 'permission',

  state: {
    dataUser: {
      list: [],
      pagination: {},
    },
    dataRole: {
      list: [],
      pagination: {},
    },
    dataRight: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetchUser({ payload }, { call, put }) {
      const response = yield call(queryUser, payload);
      if (response && response.code === 200) {
        yield put({
          type: 'save',
          payload: {
            dataUser: response.data,
          },
        });
      }
    },
    *addUser({ payload, callback }, { call, put }) {
      const response = yield call(addUser, payload);
      if (callback) callback(response);
    },
    *updateUser({ payload, callback }, { call, put }) {
      const response = yield call(updateUser, payload);
      if (callback) callback(response);
    },
    *removeUsers({ payload, callback }, { call, put }) {
      const response = yield call(removeUsers, payload);
      // request.js DELETE默认不返回json对象
      if (callback) callback(JSON.parse(response));
    },
    *resetPwd({ payload, callback }, { call, put }) {
      const response = yield call(resetPwd, payload);
      if (callback) callback(response);
    },
    *disableUser({ payload, callback }, { call, put }) {
      const response = yield call(disableUser, payload);
      if (callback) callback(response);
    },
    *fetchRole({ payload }, { call, put }) {
      const response = yield call(queryRole, payload);
      if (response && response.code === 200) {
        yield put({
          type: 'save',
          payload: {
            dataRole: response.data,
          },
        });
      }
    },
    *fetchUserRole({ payload, callback }, { call }) {
      const response = yield call(queryUserRole, payload);
      if (response && response.code === 200) {
        if (callback) callback(response.data);
      }
    },
    *setUserRole({ payload, callback }, { call, put }) {
      const response = yield call(setUserRole, payload);
      if (callback) callback(response);
    },
    *addRole({ payload, callback }, { call, put }) {
      const response = yield call(addRole, payload);
      if (callback) callback(response);
    },
    *updateRole({ payload, callback }, { call, put }) {
      const response = yield call(updateRole, payload);
      if (callback) callback(response);
    },
    *fetchRight({ payload }, { call, put }) {
      const response = yield call(queryRight, payload);
      if (response && response.code === 200) {
        yield put({
          type: 'save',
          payload: {
            dataRight: response.data,
          },
        });
      }
    },
    *fetchRoleRight({ payload, callback }, { call }) {
      const response = yield call(queryRoleRight, payload);
      if (response && response.code === 200) {
        if (callback) callback(response.data);
      }
    },
    *setRoleRight({ payload, callback }, { call, put }) {
      const response = yield call(setRoleRight, payload);
      if (callback) callback(response);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
